package com.cn.dream_order_server.async;


import com.cn.dream_order_server.mapper.OrderLogisticsMapper;
import com.cn.dream_order_server.pojo.LogisticsManagement;
import com.cn.dream_order_server.pojo.OrderLogistics;
import com.cn.dream_order_server.pojo.OrderOrder;
import com.cn.dream_order_server.util.DoConfirmReceiptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
@Async
public class WlcLogisticsManagementAsync {
    String[] address = {"深圳","上海","湖南","武汉","天津","南京"};
    String[] address2 = {"盐城","孝感","咸宁","南昌","安徽","成都"};
    String[] address1 = {"汶川","黑龙江","沈阳","北京","呼和浩特","广东"};


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private WlcDoConfirmReceiptUtilAsync wlcDoConfirmReceiptUtilAsync;




    public void randomLogisticsManagement(OrderOrder orderOrder){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String s1 = format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT1;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1);
        System.out.println(s1);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String a1 = address[(int)(Math.random()*5)];
        String s2 = format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT6+"香港行政区."+LogisticsManagement.LOGISTICSMANAGEMENT3+a1;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2);
        System.out.println(s2);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s3 =format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT2+a1;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3);
        System.out.println(s3);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String a2 = address1[(int)(Math.random()*5)];
        String s4 =format.format(new Date())+"*"+ LogisticsManagement.LOGISTICSMANAGEMENT6+a1+"."+LogisticsManagement.LOGISTICSMANAGEMENT3+a2;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4);
        System.out.println(s4);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s5 =format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT2+a2;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5);
        System.out.println(s5);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String a3 = address2[(int)(Math.random()*5)];
        String s6 = format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT6+a2+"."+LogisticsManagement.LOGISTICSMANAGEMENT3+a3;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6);
        System.out.println(s6);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s7 =format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT2+a3;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7);
        System.out.println(s7);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s8=format.format(new Date())+"*"+ LogisticsManagement.LOGISTICSMANAGEMENT6+a3+"."+LogisticsManagement.LOGISTICSMANAGEMENT3+orderOrder.getUserSite().getSiteDistrict();
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+","+s8);
        System.out.println(s8);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s9 = format.format(new Date())+"*"+LogisticsManagement.LOGISTICSMANAGEMENT2+orderOrder.getUserSite().getSiteDistrict();
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+","+s8+","+s9);
        System.out.println(s9);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s10 =format.format(new Date())+"*"+ LogisticsManagement.LOGISTICSMANAGEMENT4;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+","+s8+","+s9+","+s10);
        System.out.println(s10);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String s11 =format.format(new Date())+"*"+ LogisticsManagement.LOGISTICSMANAGEMENT5;
        stringRedisTemplate.opsForValue().set("LogisticsManagement-"+orderOrder.getOrderId(),s1+","+s2+","+s3+","+s4+","+s5+","+s6+","+s7+","+s8+","+s9+","+s10+","+s11);
        System.out.println(s11);
        OrderLogistics orderLogistics = orderLogisticsMapper.selectByPrimaryKey(orderOrder.getOrderOther1());
        if (orderLogistics!=null){
            orderLogistics.setLogisticsstatusId("4");
            orderLogistics.setDeliveryTime(new Date());
            int i = orderLogisticsMapper.updateByPrimaryKeySelective(orderLogistics);
            if(i==1){
                DoConfirmReceiptUtil myCacheUtil = new DoConfirmReceiptUtil(wlcDoConfirmReceiptUtilAsync);
                myCacheUtil.set(orderOrder.getOrderId(),orderOrder,30);
            }
        }

    }



}
